//
//  HLTabBar.m
//  HL-百思不得姐
//
//  Created by admin on 16/4/27.
//  Copyright © 2016年 CHL. All rights reserved.
//

#import "HLTabBar.h"
#import "UIView+HLFrame.h"

@interface HLTabBar()

/** tabBar上的按钮  */
@property (weak,nonatomic) UIButton *publishButton;

@end

@implementation HLTabBar

#pragma mark - 懒加载按钮
- (UIButton *)publishButton {
    if (_publishButton == nil) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [btn sizeToFit];
        [self addSubview:btn];
        
        _publishButton = btn;
    }
    return _publishButton;
}

#pragma mark - 布局tabBar上的子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //NSLog(@"%@",self.subviews);  //得到tabBar的子控件类型为UITabBarButton,能打印出来却不能敲出来,属于系统私有类
    
    NSInteger count = self.items.count;
//    NSInteger count = self.subviews.count;   //tabBar中不止UItabBarButton四个子控件,用subviews排布会错误
//    NSLog(@"-------%@",self.subviews);
//    NSLog(@"-------%@",self.items);
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = self.HL_width / (count + 1);
    CGFloat height = self.HL_height;
    int i = 0;
    for (UIView *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (i == 2) {
                i +=1;
            }
            x = i * width;
            tabBarButton.frame = CGRectMake(x, y, width, height);
            i++;
        }
    }
//    self.publishButton.frame = CGRectMake(width * 2, 0, width, height);
    self.publishButton.center = CGPointMake(self.HL_width * 0.5, self.HL_height * 0.5);
    
}

@end
